Imports System.Text


''' <summary>
''' Creates groups of queries from one set of search terms, used by the 
''' SearchBuilder for creating optimized search engine style queries.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[cinj]	16/06/2006	Created
''' </history>
Public Class SQLGrouper
    Private Shared Function doCase(ByVal input As String) As String
        Return "UPPER(" & input & ")"
    End Function

    Public Enum GROUPING
        GROUP_START
        GROUP_MID
        GROUP_END
        GROUP_EXACT
    End Enum

    Public Enum LOGIC
        LOGIC_AND
        LOGIC_OR
    End Enum

    Public Shared Function GroupTerms(ByRef terms As ArrayList, ByVal logicOp As LOGIC) As String
        Dim e As IEnumerator = terms.GetEnumerator()
        Dim termStart As Boolean = False
        Dim sb As New StringBuilder("((")
        e.Reset()
        While e.MoveNext()
            If Not CStr(e.Current).Trim() = "" Then
                If Not termStart Then
                    termStart = True
                Else
                    If logicOp = LOGIC.LOGIC_AND Then
                        sb = sb.Append(") AND (")
                    Else
                        sb = sb.Append(") OR (")
                    End If
                End If
                sb = sb.Append(CStr(e.Current))
            End If
        End While
        sb = sb.Append("))")
        Return sb.ToString()
    End Function

    Public Shared Function GroupTerms(ByVal terms() As String, ByVal logicOp As LOGIC) As String
        Dim a As New ArrayList(terms)
        Return GroupTerms(a, logicOp)
    End Function

    Public Shared Function GroupTerm(ByVal table As String, ByVal query As String, ByVal rawTerm As String, ByVal ignoreCase As Boolean, ByVal groupStyle As GROUPING) As String
        Dim exp As String = ""
        Dim term As String = UnicodeTranslator.SqlUnicodePattern(UnicodeTranslator.SqlUnicodeTranslate(rawTerm))
        Select Case groupStyle
            Case GROUPING.GROUP_MID
                exp = "'%" & Replace(term, "'", "''") & "%'"
            Case GROUPING.GROUP_END
                exp = "'%" & Replace(term.TrimEnd(), "'", "''") & "'"
            Case GROUPING.GROUP_START
                exp = "'" & Replace(term.TrimStart(), "'", "''") & "%'"
            Case GROUPING.GROUP_EXACT
                exp = "'" & Replace(term.Trim(), "'", "''") & "'"
        End Select
        Return "(" & IIf(ignoreCase, doCase(table), table) & " " & query & " " & IIf(ignoreCase, doCase(exp), exp) & ")"
    End Function

    Public Shared Function GroupAllPermutations(ByVal table As String, ByVal query As String, ByVal rawTerm As String, ByVal ignoreCase As Boolean) As String
        Dim a As New ArrayList
        a.Add(GroupTerm(table, query, rawTerm, ignoreCase, GROUPING.GROUP_MID))
        a.Add(GroupTerm(table, query, rawTerm, ignoreCase, GROUPING.GROUP_END))
        a.Add(GroupTerm(table, query, rawTerm, ignoreCase, GROUPING.GROUP_START))
        a.Add(GroupTerm(table, query, rawTerm, ignoreCase, GROUPING.GROUP_EXACT))
        Return GroupTerms(a, LOGIC.LOGIC_OR)
    End Function
End Class
